package karaoke.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import junit.framework.Assert;

import karaoke.excepcion.DAOExcepcion;
import karaoke.modelo.JsonResult;
import karaoke.modelo.Local;
import karaoke.modelo.Sala;
import karaoke.negocio.GestionLocales;
import karaoke.negocio.GestionSalas;

/**
 * Servlet implementation class RecuperaPostServlet
 */
@WebServlet("/admin/SalaRegistrarServlet")
public class SalaRegistrarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SalaRegistrarServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	  
       

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Gson gson = new Gson();
		JsonResult result = new JsonResult();
		String message = "";
		int tipoMsg = JsonResult.WARNING;
	    String codigoSala = request.getParameter("RegCodigo");
  
	    String nombreSala = request.getParameter("RegNombre");
	    String descripcionSala = request.getParameter("RegDescripcion");
	    int capacidadSala = Integer.parseInt(request.getParameter("RegCapacidad"));
	 
	    int idLocal = Integer.parseInt(request.getParameter("RegidLocal"));
	    boolean eLog=false;
	    
        Sala sala = null;
		GestionSalas negocio = new GestionSalas();
		GestionLocales negocioLocal = new GestionLocales();
		try {
			Local local=negocioLocal.obtener(idLocal);
			Sala nuevo =negocio.insertar(codigoSala,nombreSala,capacidadSala,descripcionSala,eLog,local);
		    if (nuevo.getIdSala()>0) {
					tipoMsg = JsonResult.SUCCESS;
					message = "["+nuevo.getIdSala()+"] Sala se grabo satisfactoriamente";
				} else {
					tipoMsg = JsonResult.ERROR;
					message = "No se pudo registrar";
			}
			
		} catch (DAOExcepcion e) {
			tipoMsg = JsonResult.ERROR;

			message = e.getMessage();
	
		}
		
		// Guardando datos en el scope REQUEST
	
     	result.addMenssage(tipoMsg, message);
		
		PrintWriter out = response.getWriter();
		out.println(gson.toJson(result));       
	
   } //DoPost
     

	
}
